package com.ay.wiki_plt.controller;

import com.ay.wiki_plt.common.PageBean;
import com.ay.wiki_plt.common.Result;
import com.ay.wiki_plt.domain.Ebook;
import com.ay.wiki_plt.domain.dto.PageRequestDTO;
import com.ay.wiki_plt.service.EbookService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/ebook")
public class EbookController {


    @Autowired
    private EbookService ebookService;


    /*查询所有电子书列表 分页展示*/
    @GetMapping("/list")
    public Result list(
            @Valid PageRequestDTO pageRequestDTO,
            @RequestParam(defaultValue = "") String name,
            @RequestParam(defaultValue = "") Long category2Id) {

        log.info("查询电子书列表,模糊查询:{},{},{},{}", pageRequestDTO.getPage(), pageRequestDTO.getPageSize(), name,category2Id);
        PageBean pageBean = ebookService.list(pageRequestDTO.getPage(), pageRequestDTO.getPageSize(), name,category2Id);
        return Result.success(pageBean);
    }


    /*根据id查询电子书*/
    @GetMapping("/{id}")
    public Result listOne(@PathVariable Integer id) {
        log.info("查询的id:{}", id);
        Ebook ebook = ebookService.listOne(id);
        return Result.success(ebook);
    }


    //电子书保存接口
    @PostMapping
    public Result save(@RequestBody Ebook ebook){
        log.info("保存电子书:{}",ebook);
        //调用service
        ebookService.save(ebook);
        return Result.success();
    }

    //删除电子书
    //参数传递 /1,2,3
    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Long> ids){
        log.info("删除的电子书id:{}",ids);
        ebookService.delete(ids);
        return Result.success();
    }



}
